/**
 * 
 */
package it.irondev.appturismo.action;

import it.irondev.appturismo.entity.Accomodation;
import it.irondev.appturismo.entity.Accomodationtype;
import it.irondev.appturismo.entity.City;
import it.irondev.appturismo.entity.Gallery;
import it.irondev.appturismo.entity.Language;
import it.irondev.appturismo.filter.BaseFilter;
import it.irondev.appturismo.service.IAccomodationsService;
import it.irondev.appturismo.service.ICityService;
import it.irondev.appturismo.service.IGalleryService;
import it.irondev.appturismo.service.ILanguageService;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 * @author federicocerruto
 *
 */
@Controller("accomodationAction")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class AccomodationAction extends AbstractAction {



	/**
	 * 
	 */
	private static final long serialVersionUID = 5268509816751104323L;

	@Autowired
	IAccomodationsService serviceAccomodation;

	@Autowired
	ILanguageService serviceLanguage;

	@Autowired
	ICityService serviceCity;

	@Autowired
	IGalleryService galleryService;

	private Integer idGallery;
	private Long idAccomodation;
	private List<Language> listLanguage;
	private List<Accomodationtype> listType;
	private List<City> listCity;
	private Accomodation entityBean;
	private List<Accomodation> accomodations;
	
	private String stringaSearchLanguage;
	

	public String list() {
		listLanguage = serviceLanguage.getAllLanguages();
		setStringaSearchLanguage(buildStringSearch(listLanguage));
		return "listAccomodation";
	}
	
	public String listJson() {
		accomodations = serviceAccomodation.getAllAccomodations();
		return "listAccomodationJson";
	}


	//	@Action(value="viewAccomodation",results=@Result(name="succes",location="example",type="tiles"))
	public String view() {
		setEntityBean(serviceAccomodation.getAccomodationById(idAccomodation.intValue()));
		return "success";
	}


	public String nuovo() {
		listLanguage = serviceLanguage.getAllLanguages();
		listType = serviceAccomodation.getAllAccomodationType();
		listCity = serviceCity.getAllCity();
		return "newAccomodation";
	}

	
	
	public String edit() {
		listLanguage = serviceLanguage.getAllLanguages();
		listType = serviceAccomodation.getAllAccomodationType();
		listCity = serviceCity.getAllCity();
		entityBean = serviceAccomodation.getAccomodationById(idAccomodation.intValue());
		return "editAccomodation";
	}
	
	
	private String buildStringSearch(List<Language> listLanguage2) {
		String stringa = "{value:'";
		for (Language language : listLanguage2) {
			stringa+=language.getName()+":"+language.getName()+";";
		}
		stringa+="'}";
		return stringa;
	}

	public void delete() {
		if (getOper().equalsIgnoreCase("del")) {
			int idEntity = Integer.parseInt(getId());
			serviceAccomodation.deleteAccomodation(idEntity);
		}
	}

	public String update() {
		serviceAccomodation.updateAccomodation(entityBean);
		return "listAccomodation";
	}
	
	
	public String add() {
		serviceAccomodation.addAccomodation(entityBean);
		return "listAccomodation";
	}

	
	public String addGallery() {
		Gallery tmpGallery = new Gallery();
		galleryService.addGallery(tmpGallery);
		idGallery = tmpGallery.getIdgallery();
		entityBean = serviceAccomodation.getAccomodationById(idAccomodation.intValue());
		entityBean.setGallery(galleryService.getGalleryById(idGallery));
		serviceAccomodation.updateAccomodation(entityBean);
		return "addAccomodationGallery";
	}

	
	public String listMobile() {
		BaseFilter filter = new BaseFilter();
		String ln = getLocale().getLanguage();
		filter.setIdlanguage(serviceLanguage.getLanguageByPrefix(ln).getIdlanguage());
		filter.setFavouriteCity(getFavouriteCity());
		accomodations = serviceAccomodation.getAccomodationByFilter(filter);
		return "listAccomodationMobile";
	}
	
	public String detailMobile() {
		BaseFilter filter = new BaseFilter();
		filter.setId(idAccomodation.intValue());
		List<Accomodation> tmpAccomodations = serviceAccomodation.getAccomodationByFilter(filter);
		if(tmpAccomodations.size()>0)
			entityBean = tmpAccomodations.get(0);
		return "detailAccomodationMobile";
	}
	
	
	
	public Long getIdAccomodation() {
		return idAccomodation;
	}



	public void setIdAccomodation(Long idAccomodation) {
		this.idAccomodation = idAccomodation;
	}



	public List<City> getListCity() {
		return listCity;
	}


	public void setListCity(List<City> listCity) {
		this.listCity = listCity;
	}


	public Accomodation getEntityBean() {
		return entityBean;
	}


	public void setEntityBean(Accomodation entityBean) {
		this.entityBean = entityBean;
	}


	public List<Accomodation> getAccomodations() {
		return accomodations;
	}


	public void setAccomodations(List<Accomodation> accomodations) {
		this.accomodations = accomodations;
	}

	public List<Language> getListLanguage() {
		return listLanguage;
	}


	public void setListLanguage(List<Language> listLanguage) {
		this.listLanguage = listLanguage;
	}


	public List<Accomodationtype> getListType() {
		return listType;
	}


	public void setListType(List<Accomodationtype> listType) {
		this.listType = listType;
	}

	public String getStringaSearchLanguage() {
		return stringaSearchLanguage;
	}

	public void setStringaSearchLanguage(String stringaSearchLanguage) {
		this.stringaSearchLanguage = stringaSearchLanguage;
	}

	/**
	 * @return the idGallery
	 */
	public Integer getIdGallery() {
		return idGallery;
	}

	/**
	 * @param idGallery the idGallery to set
	 */
	public void setIdGallery(Integer idGallery) {
		this.idGallery = idGallery;
	}
	
	
	
}
